﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class WeaponSlot : Part
{
    public enum ParameterType
    {
        NONE,
        Switch,
        Power,
        ParameterTypeEnd,
    }

    public Weapon.Level level { get { return m_Level; } }
    public int group { get { return m_Group; } set { m_Group = value; } }
    public Weapon weapon { set { m_Weapon = value; } get { return m_Weapon; } }

    public Callback onFire;
    Weapon m_Weapon;
    int m_Group = 0;
    Weapon.Level m_Level;

    float m_Power = 0;
    float m_PowerMax = 1;

    public float power
    {
        set { m_Power = value; }
        get { return m_Power; }
    }
    public float powerMax
    {
        set { m_PowerMax = value; }
        get { return m_PowerMax; }
    }
    public WeaponSlot()
    {
        m_Type = ItemType.WEAPON_SLOT;
    }

    public void Fire()
    {
        if(onFire != null)
            onFire();
    }
    public override void SetParameter(BehaviourMate behaviour)
    {
        switch ((ParameterType)behaviour.m_ID)
        {
            case ParameterType.Switch:
                isEnabled = behaviour.m_TargetData.m_TargetDataBool;
                break;
            case ParameterType.Power:
                Fire();
                break;
        }
        return;
    }
    public override int GetParameterNumber()
    {
        return (int)ParameterType.ParameterTypeEnd;
    }

    public override string GetParameterName(int type)
    {
        string name = "无";
        switch ((ParameterType)type)
        {
            case ParameterType.Switch:
                name = "开关";
                break;
            case ParameterType.Power:
                name = "开火";
                break;
        }
        return name;
    }
    public override BehaviourMate GetDefaultInfoOfParameter(int type)
    {
        var name = GetParameterName(type);
        if (name == "无")
            return null;
        var commonData = new CommonData();
        BehaviourMate defaultInfo = new BehaviourMate(name, type, commonData);
        defaultInfo.m_ID = type;
        switch ((ParameterType)type)
        {
            case ParameterType.Switch:
                commonData.m_DataType = CommonData.DataType.Bool;
                break;
            case ParameterType.Power:
                commonData.m_DataType = CommonData.DataType.NONE;
                break;
        }
        return defaultInfo;
    }

    public override JsonData Save()
    {
        JsonData root = base.Save();
        if(m_Weapon != null)
            root["WeaponID"] = m_Weapon.typeID;
        return root;
    }

    public override bool Load(JsonData json)
    {
        base.Load(json);
        IDictionary<string, JsonData> jsond = json.ValueAsObject();
         
        if (jsond.ContainsKey("WeaponID"))
            m_Weapon = ItemManager.Instance.createItem(jsond["WeaponID"].ValueAsInt()) as Weapon;
        return true;
    }
}